package com.dsbj.mp.icardservice.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.mp.icardservice.entity.Employee;
import com.dsbj.mp.icardservice.entity.EmployeeWelfare;
import com.dsbj.mp.icardservice.entity.EmployeeWorkStatus;
import com.dsbj.mp.icardservice.entity.Persont;
import com.dsbj.mp.icardservice.mapper.EmployeeMapper;
import com.dsbj.mp.icardservice.mapper.EmployeeWelfareMapper;
import com.dsbj.mp.icardservice.mapper.PersontMapper;
import com.dsbj.mp.icardservice.service.mcommon.McommonService;
import com.dsbj.mp.icardservice.utils.entity.EmployeeCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author cjw 2019-09-24
 */
@Service
@Transactional
public class EmployeeService extends ServiceImpl<EmployeeMapper, Employee> {

	@Resource
	private EmployeeMapper employeeMapper;

	@Resource
	private EmployeeWelfareMapper employeeWelfareMapper;
	
	@Resource
	private PersontMapper persontMapper;
	
	@Autowired
	private EmployeeWorkStatusService employeeWorkStatusService;


	/**
	 * 根据employeeno查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	public Employee getEmployeeByNo(String companyno, String employeeno){

		//QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		//queryWrapper.eq("EMPLOYEENO",employeeno);
		//Employee employee=employeeMapper.selectOne(queryWrapper);

		Employee employee1=employeeMapper.findByCompanynoAndEmployeeno(companyno,employeeno);
		return employee1;
	}

	/**
	 * 根据employeeno查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-23
	 */
	public Employee getEmployeeByNo(String employeeno){
		Employee employee1=employeeMapper.findByEmployeeno(employeeno);
		return employee1;
	}

	/**
	 * 根据phoneNum获取对应的employee信息。
	 * @deprecated 待删
	 * @param
	 * @return
	 * @author cjw 2019-10-18
	 */
	@Deprecated
	public ResponseMessage getEmployeeByPhoneNum_0(String phoneNum){
		//获取employeeWelfare信息
		EmployeeWelfare employeeWelfare=employeeWelfareMapper.selectOne(
				new QueryWrapper<EmployeeWelfare>().eq("CONTACTWAY",phoneNum).eq("DELETESTATUS",1)
		);
		//若employeeWelfare为null
		if(employeeWelfare==null){
			return ResponseMessage.error("未找到该手机号码对应的员工信息!");
		}

		//根据employeeWelfare.employeeno获取employee信息
		Employee employee=getEmployeeByNo(employeeWelfare.getEmployeeno());
		return ResponseMessage.ok(employee);
	}

	/**
	 * 根据手机号码，获取"当前有效的"员工信息。
	 * @param
	 * @return
	 * @author cjw 2019-10-18
	 */
	public ResponseMessage getEmployeeByPhoneNum(String phoneNum){
		//根据手机号码，获取"当前有效的"员工信息
		Employee employee=employeeMapper.findAliveByContactway(phoneNum);

		//若employee为null
		if(employee==null){
			return ResponseMessage.error("未找到该手机号码对应的员工信息!");
		}
		return ResponseMessage.ok(employee);
	}

	/**
	 * 根据employeenameDim查询employee信息。
	 * @deprecated 待删
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmployeeByEmployeenameDim_0(String companyno, String employeenameDim){

		/* //连表查询
		Employee employee_input=new Employee();
		employee_input.setEmployeenameDim(employeenameDim);
		employee_input.setDeleteStatus(1);

		List<Employee> list_employee=null;
		list_employee=(List<Employee>) employeeMapper.findByMap(ObjectParse.convertObject_toMap(employee_input));
		return list_employee;
		*/

		//未联表查询
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.and(i->i.like("EMPLOYEENO",employeenameDim).or().like("EMPLOYEENAME",employeenameDim))
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1);

		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}

	/**
	 * 根据employeenameDim模糊查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmplByEmployeenameDim(String companyno, String employeenameDim){
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.apply("UPPER(EMPLOYEENAME) LIKE '%'||UPPER({0})||'%'", employeenameDim)
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1).apply("ROWNUM<100");

		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}

	/**
	 * 根据employeenoDim模糊查询employee信息。
	 * @param
	 * @return
	 * @author cjw 2019-09-24
	 */
	public List<Employee> getEmplByEmployeenoDim(String companyno, String employeenoDim){
		QueryWrapper<Employee> queryWrapper=new QueryWrapper<>();
		queryWrapper.select("ID","EMPLOYEENO","EMPLOYEENAME").eq("COMPANYNO",companyno)
				.apply("UPPER(EMPLOYEENO) LIKE '%'||UPPER({0})||'%'", employeenoDim)
				.isNull("DISMISSIONDATE").eq("DELETESTATUS",1).apply("ROWNUM<100");

		List<Employee> employees=employeeMapper.selectList(queryWrapper);
		return employees;
	}


	/**
	 * 获取数据库时间
	 * @return
	 */
	public Date getDatabaseDate(){
		return employeeMapper.findDatabaseDate();
	}
	
	/**
	 * 获取复工状态(0 不可复工; 1 可复工;)。
	 * @param idCardNo
	 * @return
	 */
	public ResponseMessage getReturnWorkState(String idCardNo) {
		//判断是否能复工
		QueryWrapper<EmployeeWorkStatus> queryWrapperTemp = new QueryWrapper<EmployeeWorkStatus>()
				.eq("IDCARDNO", idCardNo)
				.eq("STATUS",1);
		EmployeeWorkStatus employeeWorkStatus = employeeWorkStatusService.getOne(queryWrapperTemp);
		//若可复工
		if(employeeWorkStatus!=null){
			return ResponseMessage.ok(1);
		}else{
			//若不可复工
			return ResponseMessage.ok(0);
		}
	}
	
	/**
	 * 获取"所有在职员工"的身份证号码。查询了h_p_employee,h_p_persont两张表。
	 * @param
	 * @return
	 * @author cjw 2020-02-25
	 */
	public List<String> getEmployeeIdcardno(){
		//从employee表中获取在职员工身份证号等信息
		List<Employee> list_employee=null;
		QueryWrapper<Employee> queryWrapper_employee=new QueryWrapper<Employee>()
				.select("IDCARDNO","COMPANYNO","EMPLOYEENO","EMPLOYEENAME")
				.isNull("DISMISSIONDATE")
				.eq("DELETESTATUS",1)
				.orderByAsc("COMPANYNO","EMPLOYEENO");
		list_employee=employeeMapper.selectList(queryWrapper_employee);
		
		//从persont表中获取在职员工身份证号等信息
		List<Persont> list_persont=null;
		QueryWrapper<Persont> queryWrapper_persont=new QueryWrapper<Persont>()
				.select("IDCARDNO","COMPANYNO","EMPLOYEENO","EMPLOYEENAME")
				.eq("STATUS",1)
				.orderByAsc("COMPANYNO","EMPLOYEENO");
		list_persont= persontMapper.selectList(queryWrapper_persont);
		
		//合并list_employee,list_persont中的员工信息,并去除重复的员工信息。
		List<EmployeeCommon> list_employeeCommon=null;
		list_employeeCommon= McommonService.getEmployeeCommon_fromEmployeeAndPersont(list_employee,list_persont);
		
		//返回"员工身份证号码列表"
		List<String> list_idcardno=list_employeeCommon.stream().map(EmployeeCommon::getIdcardno).collect(Collectors.toList());
		return list_idcardno;
	}
	
	
	
}
